﻿using Funq;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Data;
using ServiceStack.Auth;
using ServiceStack.Caching;
using EquipmentManager.Services;

namespace EquipmentManager
{
    public class AppHost : AppSelfHostBase
    {
        /// <summary>
        /// Default constructor.
        /// Base constructor requires a name and assembly to locate web service classes. 
        /// </summary>
        public AppHost()
            : base("EquipmentManager", typeof(MyService).Assembly)
        {
            //Container.Register<ILog>(LogManager.GetLogger(Settings1.Default.WebServiceUsingLogger));
        }

        //private IAuthSession SessionFactory()
        //{
        //    return new AuthUserSession();
        //}

        /// <summary>
        /// Application specific configuration
        /// This method should initialize any IoC resources utilized by your web service classes.
        /// </summary>
        /// <param name="container"></param>
        public override void Configure(Container container)
        {
            //var authProviders = new List<IAuthProvider>();
            //authProviders.Add(new CredentialsAuthProvider());
            //var authFeature = new AuthFeature(SessionFactory, authProviders.ToArray());
            //Plugins.Add(authFeature);

            //var dbFactory = new OrmLiteConnectionFactory("~/db.sqlite".MapHostAbsolutePath(), SqliteDialect.Provider);
            //container.Register<IDbConnectionFactory>(dbFactory);

            //var authRepo = new OrmLiteAuthRepository(dbFactory);
            //container.Register<IUserAuthRepository>(authRepo);

            //container.Register<ICacheClient>(new MemoryCacheClient());

            //this.Plugins.Add(new RegistrationFeature());

            //Plugins.Add(new RazorFormat());  //增加支持Razor页面
            //增加支持不同后缀名的静态文件
            SetConfig(new HostConfig
            {
                AllowFileExtensions = { "properties", "zip", "json", "library", "theming", "theme" }
            });
            SetConfig(new HostConfig() { DefaultRedirectPath = "/index.html" });
            container.Register<IDbConnectionFactory>(GlobalConfig.DbConnectionFactory);
            //Config examples
            //this.Plugins.Add(new PostmanFeature());
            //this.Plugins.Add(new CorsFeature());
            //Plugins.Add(new RazorFormat());  //增加支持Razor页面
            //Plugins.Add(new ValidationFeature()); //增加支持Validate功能
            //SetConfig(new HostConfig() { DefaultRedirectPath = "/content/default.html" }); //设置默认首页为/content/default.html
            //OrmLiteConnectionFactory factory = null;
            /*if (Settings1.Default.DataBaseSelect == "sqlite")
            {
                factory = new OrmLiteConnectionFactory(
                    Settings1.Default.SqliteConn.MapAbsolutePath(), SqliteDialect.Provider);
                container.Register<IDbConnectionFactory>(factory);
                TestingCollection.Db = factory.Open();
            }
            else if (Settings1.Default.DataBaseSelect == "mysql")
            {
                factory = new OrmLiteConnectionFactory(
                    Settings1.Default.MySqlConn, MySqlDialect.Provider);
                container.Register<IDbConnectionFactory>(factory);
                TestingCollection.Db = factory.Open();
            }
            else if (Settings1.Default.DataBaseSelect == "sqlserver")
            {
                factory = new OrmLiteConnectionFactory(
                    Settings1.Default.SqlServerConn, SqlServerDialect.Provider);
                container.Register<IDbConnectionFactory>(factory);
                TestingCollection.Db = factory.Open();
            }
            else
            {
                throw new Exception(String.Format("数据库错误设置为{0}", Settings1.Default.DataBaseSelect));
            }*/



        }

    }
}
